﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; 

namespace StartStop.Helpers
{
    public  class Devs
    {

        public const string LiveConnectionString = "data source=v2.startstop.me;initial catalog=startstop;persist security info=True;user id=sa;password=PASSWORD;";

        public static  ServiceResources.MessageResponse  UpdateDeveloper(ServiceResources.Developer oDev) 
        {

            // TO DO! 
            // We need to add the modify code, so check to see if the devid has been passed. 
            // Also we should check that the user hasn't already got a dev record, so we do both. 

            ServiceResources.MessageResponse oResponse = new ServiceResources.MessageResponse();
            oResponse.Success = false;
            string _result = ""; 


            try
            {

                bool _isUpdate = false;
                if (StartStop.Helpers.Sql.ExecuteScalar("select DeveloperID from StartStop.dbo.Developers where UserID = " + oDev.UserID.ToString()) != "")
                {
                    _isUpdate = true; 
                }


                SqlConnection oConn = new SqlConnection(LiveConnectionString);
                oConn.Open(); 
                SqlCommand oCmd = new SqlCommand(@"
                                Insert into Developers ( SiteURL,TypeID, CreatedOn, ModifiedOn, Lastaccessed, Approved, Spam,  Description, UserID)
                                values
                                (@SiteURL,@TypeID,GetdAte(),@ModifiedOn,GetDate(),@Approved,@Spam,@Description, @UserID);
                                 select @@IDENTITY;
                                ", oConn);


                oCmd.Parameters.AddWithValue("@SiteURL", oDev.SiteURL);
                oCmd.Parameters.AddWithValue("@TypeID", (int)oDev.Type);
                oCmd.Parameters.AddWithValue("@ModifiedOn", System.DBNull.Value);

                if (oDev.Approved != null)
                {
                    oCmd.Parameters.AddWithValue("@Approved", oDev.Approved);
                }
                else
                {
                    oCmd.Parameters.AddWithValue("@Approved", false);
                }
                oCmd.Parameters.AddWithValue("@Spam", false);
                oCmd.Parameters.AddWithValue("@Description", oDev.Description);
                oCmd.Parameters.AddWithValue("@UserID", oDev.UserID);

                 _result = oCmd.ExecuteScalar().ToString();

                 oResponse.Success = true; 
            }
            catch (Exception ex) 
            {

                StartStop.ServiceResources.ErrorEvent oError = new StartStop.ServiceResources.ErrorEvent();
                oError.CreatedOn = DateTime.Now;
                oError.ErrorDetail = ex.Message.ToString();
                oError.ErrorLevel = StartStop.ServiceResources.ErrorEvent.ErrorLevelType.Critical;
                oError.ErrorMessage = "Cannot write developer record";

                StartStop.Helpers.EventLog.LogError(oError); 
            }

            return oResponse; 
        }

        public static ServiceResources.MessageResponse IsDevRegistered(Int64 UserID)
        {
            ServiceResources.MessageResponse oResponse = new ServiceResources.MessageResponse();
            oResponse.Success = false;
            try
            {
                string _result = Sql.ExecuteScalar("select DeveloperID from Developers where Userid=@passedvalue", UserID.ToString());
                if (_result != "")
                {
                    oResponse.Success = true;
                    oResponse.ReturnID = Int64.Parse(_result);
                    oResponse.Message = "User found";
                }
            }
            catch (Exception ex)
            {
                ServiceResources.ErrorEvent oError = new ServiceResources.ErrorEvent();
                oError.CreatedOn = DateTime.Now;
                oError.ErrorDetail = ex.Message;
                oError.ErrorLevel = ServiceResources.ErrorEvent.ErrorLevelType.Critical;
                oError.ErrorMessage = "Check user is DEV fails";
                oError.Method = "StartStop.Helpers.Dev.IsDevRegistered"; 

                StartStop.Helpers.EventLog.LogError(oError); 

            }
            return oResponse; 
        }
        

    }
}
